package com.lwj.project1.RecommendPageFragments;

import android.content.Context;
import android.media.Image;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.lwj.project1.R;
import com.lwj.project1.databaseHelper.ImageUtil;
import com.lwj.project1.entity.GameCard;

import java.util.List;

public class MyGameAdapter extends BaseAdapter {
    private List<GameCard> gameCards;
    private Context mContext;
    private LayoutInflater mInflater;

    public MyGameAdapter(Context context, List<GameCard> gameCards){
        mInflater = LayoutInflater.from(context);
        this.mContext = context;
        this.gameCards = gameCards;
    }
    @Override
    public int getCount() {
        return gameCards.size();
    }

    @Override
    public Object getItem(int position) {
        return gameCards.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null){
            convertView = mInflater.inflate(R.layout.game_card,parent,false);
            viewHolder = new ViewHolder();
            viewHolder.mIvImg = convertView.findViewById(R.id.game_img);
            viewHolder.mTvTitle = convertView.findViewById(R.id.game_title);
            viewHolder.mTvContent = convertView.findViewById(R.id.game_introduction);
            convertView.setTag(viewHolder);
        }else{
            viewHolder = (ViewHolder) convertView.getTag();
        }
        GameCard gameCard = gameCards.get(position);
        if (gameCard.getImg() != null){
            viewHolder.mIvImg.setImageBitmap(ImageUtil.base64ToImage(gameCard.getImg()));

        }
        viewHolder.mTvTitle.setText(gameCard.getGameName());
        viewHolder.mTvContent.setText(gameCard.getGameIntroduction());
        return convertView;
    }
    public static class ViewHolder{
        ImageView mIvImg;
        TextView mTvTitle;
        TextView mTvContent;
    }
}
